package org.example.dic.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.dic.core.entity.DicInfo;
import org.example.dic.core.entity.DicType;
import org.example.dic.core.mapper.DicInfoMapper;
import org.example.dic.core.mapper.DicTypeMapper;
import org.example.system.common.dto.dic.DicInfoDto;
import org.example.system.common.utils.JoeBeanCopyUtil;
import org.springframework.cache.annotation.CachePut;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字典缓存更新
 */
@Service
public class DicCacheService {
    @Resource
    DicTypeMapper dicTypeMapper;
    @Resource
    DicInfoMapper dicInfoMapper;

    /**
     * 更新字典类型缓存
     * @param groupKey
     * @return
     */
    @Async
    @CachePut(value = "cache1",key = "'dic:queryDicTypeList_'+#groupKey")
    public List<DicType> dicTypeCacheUpdate(String groupKey) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("groupKey",groupKey);
        qw.orderByAsc("sortNum");
        List list = dicTypeMapper.selectList(qw);
        return list;
    }
    /**
     * 更新字典信息缓存
     * @param typeKey
     * @return
     */
    @Async
    @CachePut(value = "cache1",key = "'dic:queryDicInfoList_'+#typeKey")
    public List<DicInfoDto> dicInfoListCacheUpdate(String typeKey) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("typeKey",typeKey);
        qw.orderByAsc("sortNum");
        List<DicInfo> dicList = dicInfoMapper.selectList(qw);
        return JoeBeanCopyUtil.create().copyList(dicList, DicInfoDto.class);
    }
}
